package net.wenku8.reader.view.file

import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Add
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import net.wenku8.reader.ui.lite.LitePanel
import net.wenku8.reader.ui.lite.iconMinSize
import net.wenku8.reader.ui.lite.iconSize
import net.wenku8.reader.ui.lite.onClick
import net.wenku8.reader.view.common.ConfirmLayer
import net.wenku8.reader.view.common.ConfirmViewModel


@Composable
fun FileBookcase() {
    val fileBookcaseViewModel = viewModel<FileBookcaseViewModel>()
    val confirmViewModel = viewModel<ConfirmViewModel>()
    val launcher = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
        fileBookcaseViewModel.addBookFile(uri)
    }
    LaunchedEffect(Unit) {
        fileBookcaseViewModel.initData()
    }
    LitePanel(
        hostState = fileBookcaseViewModel.hostState,
        title = "本地文件",
        dataLoading = fileBookcaseViewModel.dataLoading,
        actions = {
            Icon(
                Icons.Outlined.Add, null,
                modifier = Modifier.iconSize().onClick {
                    launcher.launch("text/plain")
                }
            )
        }
    ) {
        LazyColumn{
            items(items = fileBookcaseViewModel.bookList) { bookFile ->
                Box {
                    FileBookPreview(bookFile, true)
                    Row(
                        modifier = Modifier.fillMaxWidth().padding(top = 40.dp, end = 20.dp),
                        horizontalArrangement = Arrangement.End,
                        verticalAlignment = Alignment.CenterVertically,
                    ) {
                        Icon(
                            modifier = Modifier.iconMinSize()
                                .onClick {
                                    fileBookcaseViewModel.delBookId = bookFile.bookId
                                    confirmViewModel.show("你确定删除本书吗？"){
                                        fileBookcaseViewModel.delBookFile()
                                    }
                            },
                            imageVector = Icons.Outlined.Delete,
                            contentDescription = null
                        )
                    }
                }
            }
        }
        ConfirmLayer()
    }

}